/**
 * @Project POL3Cay
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Feb 23, 2011
 * @Createtime 11:26:01 AM
 */

package com.xwg.cards
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	public class BaseHand extends EventDispatcher implements IEventDispatcher
	{
		private var _cardsSet:Vector.<Card>;

		private var _numCards:int; // number of card on hand

		private var _enable:Boolean;

		public function BaseHand()
		{
			super();

			_cardsSet = new Vector.<Card>();

			for (var i:uint = 0; i < Deck.SIZE; i++)
			{
				_cardsSet[i] = new Card();
			}

			_numCards = 0;
		}

		public function resetData():void
		{
			for (var i:uint = 0; i < Deck.SIZE; i++)
			{
				_cardsSet[i].resetData();
			}

			_numCards = 0;
		}

		// TESTING
		public function sort():void
		{
			_cardsSet.sort(sortByValue);
		}

		private function sortByValue(a:Card, b:Card):int
		{
			var ret:int = 0;

			if (a.rank < b.rank)
			{
				ret = -1;
			}
			else if (a.rank > b.rank)
			{
				ret = 1;
			}
			else // a==b
			{
				if (a.suitRank < b.suitRank)
				{
					ret = -1;
				}
				else if (a.suitRank > b.suitRank)
				{
					ret = 1;
				}
				else // never reach here
				{
					ret = 0;
				}
			}

			return ret;
		}

		public function get cardsSet():Vector.<Card>
		{
			return _cardsSet;
		}


		public function set cardsSet(cardsSet:Vector.<Card>):void
		{
			_numCards = 0;

			var card:Card;

			for each (card in cardsSet)
			{
				receiveCard(card, _numCards);
			}
		}

		/**
		 * index from 0. index + 1 = position of card on hand
		 * _cardsSetRender index from 0
		 */
		public function receiveCard(card:Card, index:int, revealCard:Boolean = false, interactable:Boolean = false):void
		{
			_cardsSet[index].onHand = true;

			_cardsSet[index].visible = true;

			_cardsSet[index].revealed = revealCard;

			_cardsSet[index].interactable = interactable;

			_cardsSet[index].setCard(card.value, card.suit);

			_numCards++;
		}

		/*
		private function _stringToCard(cardString:String):Card
		{
			var value:String;

			var suitChar:String

			if (cardString.length == 3) // 10
			{
				value = "10";

				suitChar = cardString.charAt(2);
			}
			else // length == 2
			{
				value = cardString.charAt(0);

				suitChar = cardString.charAt(1);
			}

			var card:Card = new Card();
			card.value = value;
			card.suit = suitChar;

			return card;
		}
		//*/

		/* OLD
		private function _stringToCard(cardString:String):Card
		{
			var value:int;

			var suitChar:String

			if (cardString.length == 3) // 10
			{
				value = 10;

				suitChar = cardString.charAt(2);
			}
			else // length == 2
			{
				switch (cardString.charAt(0))
				{
					case "A":
						value = 1;
						break;
					case "J":
						value = 11;
						break;
					case "Q":
						value = 12;
						break;
					case "K":
						value = 13;
						break;
					default:
						value = int(cardString.charAt(0));
						break;
				}

				suitChar = cardString.charAt(1);
			}

			var suit:int = 0;

			switch (suitChar)
			{
				case '♣':
					suit = Card.CLUBS;
					break;
				case '♦':
					suit = Card.DIAMONDS;
					break;
				case '♥':
					suit = Card.HEARTS;
					break;
				case '♠':
					suit = Card.SPADES;
					break;
				default:
					break;
			}

			var card:Card = new Card();
			card.value = value;
			card.suit = suit;

			return card;
		}
		//*/

		/*
		public function receiveCardsString(cardsString:String, revealCard:Boolean = false, interactable:Boolean = false):void
		{
			var cardStrings:Array = cardsString.split(' ');

			for (var i:int = 0; i < cardStrings.length; i++)
			{
				if (i == 0) // reset rank at new turn
				{
					resetData();
				}

				receiveCard(_stringToCard(cardStrings[i]), i, revealCard, interactable); // ERR
			}
		}
		//*/

		/**
		 * set cards will be throw by a string
		 */
		/*
		public function setCardsThrowString(cardsString:String, revealCard:Boolean = true, selectCard:Boolean = true):void
		{
			var cardStrings:Array = cardsString.split(' ');

			var cardIndex:uint = 0;

			var card:Card;

			for (var i:int = 0; i < Deck.SIZE; i++)
			{
				if (_cardsSet[i].onHand)
				{
					card = _stringToCard(cardStrings[cardIndex]);

					_cardsSet[i].setCard(card.value, card.suit);
					_cardsSet[i].revealCard = revealCard;
					_cardsSet[i].selected = selectCard;

					cardIndex++;

					if (cardIndex == cardStrings.length)
					{
						break;
					}
				}
			}
		}
		//*/

		/*
		public function throwCard(visible:Boolean = true):void
		{
			for each (var card:Card in _cardsSet)
			{
				if (card.onHand && card.selected)
				{
					card.onHand = false;

					card.selected = visible; // !!!

					card.visible = visible;

					_numCards--; // NEED TEST
				}
			}
		}
		//*/

		public function hideThrewCard():void
		{
			for each (var card:Card in _cardsSet)
			{
				if (!card.onHand)
				{
					card.revealed = false;
					card.selected = false;
					card.visible = false;
				}
			}
		}

		public function get enable():Boolean
		{
			return _enable;
		}

		public function set enable(value:Boolean):void
		{
			_enable = value;

			var card:Card;

			if (enable)
			{
				for each (card in _cardsSet)
				{
					if (card.onHand)
					{
						//card._cardFace.alpha = 1;
						//this.alpha = 1;
					}
				}
			}
			else
			{
				for each (card in _cardsSet)
				{
					if (card.onHand)
					{
						//card._cardFace.alpha = 0;
						//this.alpha = .3;
					}
				}
			}
		}

		public function get numCards():int
		{
			return _numCards;
		}

		public function set numCards(value:int):void
		{
			_numCards = value;
		}

		public function toStringExt(allCard:Boolean = false):String
		{
			var ret:String = "";

			for (var i:uint = 0; i < Deck.SIZE; i++)
			{
				if (allCard)
				{
					ret += _cardsSet[i].toStringExt() + " ";
				}
				else
				{
					if (_cardsSet[i].onHand)
					{
						ret += _cardsSet[i].toStringExt() + " ";
					}
				}
			}

			return ret;
		}
	}
}
